package cn.zust.itcost.service.impl;

import cn.zust.itcost.entity.User;
import cn.zust.itcost.mapper.UserMapper;
import cn.zust.itcost.service.PredictService;
import cn.zust.itcost.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private PredictService predictService;

    @Autowired
    private UserMapper userMapper;

    // 构造函数注入PredictService
    public UserServiceImpl(PredictService predictService) {
        this.predictService = predictService;
    }

    public void doTimeSeriesAnalysis() {
        // 输入数据
        double[] inputData = {15, 11, 47, 14, 35, 46, 78, 89, 102, 115, 96, 165};
        // 预测后续五个月份的值
        int numForecasts = 5;

        // 调用PredictService进行ARIMA时间序列分析
        double[] forecastValues = predictService.arimaForecast(inputData, numForecasts);

        // 输出预测结果
        System.out.println("预测结果：");
        for (int i = 0; i < numForecasts; i++) {
            System.out.println("2023年" + (inputData.length + i + 1) + "月：" + forecastValues[i]);
        }
    }


    /**
     * trend
     * @param firstDayOfMonth
     * @param lastDayOfMonth
     * @return
     */
    @Override
    public int getUserCountByMonthRange(LocalDate firstDayOfMonth, LocalDate lastDayOfMonth) {
        int count=0;
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("settime", firstDayOfMonth, lastDayOfMonth);
        count = Math.toIntExact(userMapper.selectCount(queryWrapper));  // 使用实例变量来调用 selectCount 方法

        return count;
    }

    /**
     * trend1
     * @param firstDayOfMonth
     * @param lastDayOfMonth
     * @param userJob
     * @return
     */
    @Override
    public int getUserCountByMonthRangeandUserJob(LocalDate firstDayOfMonth, LocalDate lastDayOfMonth, String userJob) {
        int count=0;
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("settime", firstDayOfMonth, lastDayOfMonth)
                .eq("userjob", userJob);
        count = Math.toIntExact(userMapper.selectCount(queryWrapper));  // 使用实例变量来调用 selectCount 方法

        return count;
    }
}
